package com.colin.app.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.bean.ItemBean;
import com.colin.library.base.BaseFragment;
import com.colin.library.help.ImageLoader;

import java.util.List;

public class RecyclerItemBeanAdapter extends RecyclerView.Adapter<RecyclerItemBeanAdapter.ViewHolder> {

    private List<ItemBean> itemBeanList;
    private int fragment_id;
    private BaseFragment.OnFragmentListener mListener;
    private Activity activity;

    public RecyclerItemBeanAdapter(Activity activity, List<ItemBean> itemBeanList, int fragment_id, BaseFragment.OnFragmentListener mListener) {
        this.activity = activity;
        this.itemBeanList = itemBeanList;
        this.fragment_id = fragment_id;
        this.mListener = mListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(activity).inflate(R.layout.item_itembean_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final ItemBean itemBean = itemBeanList.get(position);
        ImageLoader.getInstance().loadImageByGlide(holder.image_itembean_background, itemBean.getImageLarge());
        holder.text_itembean_title.setText(itemBean.getTitle());
        holder.image_itembean_background.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mView.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.shake));
                if (null != mListener) {
                    mListener.onFragmentClick(fragment_id, true, 0, itemBean);
                }
            }
        });
        holder.text_itembean_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mView.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.shake));
                if (null != mListener) {
                    mListener.onFragmentClick(fragment_id, true, 1, itemBean);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return this.itemBeanList == null ? 0 : itemBeanList.size();
    }

    public void refresh(List<ItemBean> itemBeanList) {
        this.itemBeanList = itemBeanList;
        notifyDataSetChanged();
    }
    public void addItems(List<ItemBean> itemList) {
        if (itemList != null && itemList.size() > 0) {
            for (ItemBean itemBean : itemList) {
                if (!this.itemBeanList.contains(itemBean)) {
                    this.itemBeanList.add(itemBean);
                }
            }
            notifyDataSetChanged();
        }
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final ImageView image_itembean_background;
        public final TextView text_itembean_title;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            image_itembean_background = (ImageView) view.findViewById(R.id.image_itembean_background);
            text_itembean_title = (TextView) view.findViewById(R.id.text_itembean_title);
        }

    }
}
